<!-- 公司sop配置 -->
<template>
  <dt-modal-edit
    ref="info"
    title="任务生成及加减分规则配置"
    :label-width="100"
    okText="确定"
    :width="800"
    :loading="btnLoading"
    @cancelData="cancelData"
    @ok="onOk">
    <div class="taskInfo">
      <p>
        <span class="sp1">任务名称：</span>
        <span class="sp2">{{ currentRow.sopName }}</span>
      </p>
      <p>
        <span class="sp1">任务分类：</span>
        <span class="sp2">
          {{ $dt.dictInfo().__value('sopTaskClass', currentRow.taskClass) }}
          <em @click="classifyChange">变更分类</em>
        </span>
      </p>
      <p>
        <span class="sp1">任务加减分类型：</span>
        <span class="sp2">
          {{ $dt.dictInfo().__value('sopTaskDeductType', currentRow.taskDeductType) }}
        </span>
      </p>
      <p>
        <span class="sp1">任务责任人：</span>
        <span class="sp2">{{ people ? people : '-' }}</span>
      </p>
      <p>
        <span class="sp1">责任人范围：</span>
        <span class="sp2">
          {{ currentRow.taskScope == 1 ? '所有人员' : '指定人员' }}
        </span>
      </p>
    </div>

    <div class="downBox" v-if="currentRow.roleType == 1">
      <span class="tips">
        <Icon type="md-alert" class="tips_icon" />
        提示：您可设置当前任务是否生成及是否加减分，设置并保存后，规则将即时生效
      </span>
      <!--type=3，显示是否执行扣分，这一个选项。type = 5，显示任务是否加分，这一个选项。其他则显示是否生成任务和是否执行扣分，两个选项。-->
      <div class="conpany_sop_edit">
        <div class="taskSwitch" v-if="currentRow.type == 1">
          <span>任务是否生成</span>
          <i-switch @on-change="changeTaskSwitch" v-model="currentRow.isTaskAssess"></i-switch>
        </div>
        <div class="taskSwitch">
          <span>{{currentRow.type == 5?'任务是否加分':'任务是否扣分'}}</span>
          <i-switch
            v-model="currentRow.isAssess"
            :disabled="currentRow.type == 1 && currentRow.isTaskAssess == false"></i-switch>
        </div>
      </div>
      <div class="sync">
        <Row>
          <Col flex="100px">
            <p class="sync_txt">同步至：</p>
          </Col>
          <Col flex="auto">
            <RadioGroup v-model="isSynchronize" type="button">
              <Radio :label="1">全部案场</Radio>
              <Radio :label="0">标准案场（“自定义规则案场”不会同步）</Radio>
            </RadioGroup>
          </Col>
        </Row>
        <div class="sync_tips">注意：两种同步方式均不会对，“已撤场”、“已结案”状态的案场进行同步；</div>
      </div>
    </div>

    <div class="tips" v-show="currentRow.roleType == 2">
      <Icon type="md-alert" class="tips_icon" />
      提示：您可设置当前任务在对应业务组织下是否生成及是否扣分，设置并保存后，规则将即时生效
    </div>
    <dt-grid
      ref="table"
      v-show="currentRow.roleType == 2"
      :columns="columns"
      :height="300"
      :list="list"
      :tree="{}" />

    <task-list ref="taskList" @chooseTask="chooseTask"></task-list>
    <task-volume-set ref="taskVolumeSet" @success="taskVolumeSetSuccess"></task-volume-set>

    <special-rules ref="specialRules" @success="specialRulesSuccess"></special-rules>
  </dt-modal-edit>
</template>

<script>
  import taskList from './taskList.vue'

  import columnOpt from '../opt/taskOrgColumn'
  import sopApi from '@/biz/buildingManagement/sopConfig'

  import taskVolumeSet from './taskVolumeSet.vue'
  // import lowerOrg from './lowerOrg.vue'
  import specialRules from './specialRules.vue'
  export default {
    components: {
      taskList,
      taskVolumeSet,
      // lowerOrg,
      specialRules
    },
    data() {
      return {
        // title: '任务生成及扣分规则设置',
        info: {},
        isSynchronize: 0,
        btnLoading: false,

        // 3.8

        currentRow: {},
        orgLevel1: [],
        currentOrgId: '',
        list: [],

        columns: [],
        data: [],
        people: ''
      }
    },

    methods: {
      //单独获取任务责任人
      getLeaderMsg() {
        let params = {
          configId: this.currentRow.id,
          orgId: this.currentOrgId
        }
        sopApi
          .getLeaderMsg(params)
          .then((res) => {
            this.people = res
          })
          .catch(() => {})
      },
      cancelData() {
        this.$emit('updateList')
        this.list = []
      },
      // 批处理成功
      taskVolumeSetSuccess() {
        this.onFetch()
      },

      specialRulesSuccess() {
        this.onFetch()
      },

      getOrgAndLevel() {
        let params = {
          level: 1,
          orgId: 1
        }
        return sopApi
          .getOrgAndLevel(params)
          .then((res) => {
            this.orgLevel1 = res
            this.orgLevel1.forEach((item) => {
              item.key = item.orgId
              item.value = item.orgName
            })
            this.currentOrgId = this.orgLevel1[0].orgId
            return res
          })
          .catch(() => {})
      },

      onFetch() {
        let params = {
          configId: this.currentRow.id,
          orgId: this.currentOrgId
        }
        return sopApi
          .orgList(params)
          .then((res) => {
            res.forEach((item) => {
              // this.$refs.table.treeExpand(item)
            })
            this.list = res
          })
          .catch(() => {})
      },
      chooseTask(val) {
        this.currentRow.taskClass = val.key
        // this.currentRow.sopName = val.value
      },
      //特殊规则
      specialRules(val) {
        this.$refs.specialRules.show(val, this.currentRow)
      },
      batch() {
        this.$refs.taskVolumeSet.show(this.currentOrgId, this.currentRow)
      },
      classifyChange() {
        let sopTaskClass = this.$dt.dictInfo().sopTaskClass
        this.$refs.taskList.show('sopTaskClass', sopTaskClass, this.currentRow.taskClass)
      },
      changeTaskSwitch(data) {
        if (!this.currentRow.isTaskAssess) {
          this.currentRow.isAssess = false
        }
      },
      orgChange(val) {
        this.currentOrgId = val
        this.onFetch()
        this.getLeaderMsg()
      },
      recursive(val) {
        val.forEach((item) => {
          item.isTaskAssess = false
          item.isAssess = false
          if (item.children && item.children.len) {
            this.recursive(item.children)
          }
        })
      },
      recursiveTwo(list, info) {
        list.forEach((item) => {
          if (item.id == info.id) {
            item.isTaskAssess = info.isTaskAssess
            item.isAssess = info.isAssess
          } else {
            item.children && this.recursiveTwo(item.children, info)
          }
        })
      },
      isTaskAssessChange(val, info) {
        console.log(val, info, val.value, 'vvvvvvv')
        info.isTaskAssess = val.value
        if (!val.value) {
          info.isAssess = false
          this.list.forEach((item) => {
            if (item.orgId == info.orgId) {
              item.isAssess = false
              item.isTaskAssess = false
              item.children && item.children.length && this.recursive(item.children)
            }
          })
        }

        this.recursiveTwo(this.list, info)
        this.$refs.table.loadList(this.list)
      },
      isAssessChange(val, info) {
        this.list.forEach((item) => {
          if (item.orgId == info.orgId) {
            item.isAssess = val.value
          } else {
            this.recursiveIsAssess(val, item.children, info) // 递归处理子节点
          }
        })
        console.log(val, info, this.list, '122222222')
      },
      recursiveIsAssess(val, data, info) {
        if (!data || !data.length) return // 如果没有子节点，直接返回
        data.forEach((item) => {
          if (item.orgId == info.orgId) {
            item.isAssess = val.value
          } else {
            this.recursiveIsAssess(val, item.children, info) // 递归处理子节点
          }
        })
      },
      show(data) {
        this.currentRow = data
        if (this.currentRow.roleType == 2) {
          this.getOrgAndLevel().then((res) => {
            this.$refs.table.init(this, new columnOpt(this))
            this.onFetch()

            this.getLeaderMsg()
          })
        } else {
          this.getLeaderMsg()
        }
        this.$refs.info.show()
      },

      updateClass() {
        let params = {
          configId: this.currentRow.id,
          taskClass: this.currentRow.taskClass
        }
        return sopApi
          .updateClass(params)
          .then((res) => {
            return res
          })
          .catch(() => {})
      },

      updateOrg() {
        let params = {
          configId: this.currentRow.id,
          orgId: this.currentOrgId,
          orgList: this.list,
          updateAll: false
        }
        return sopApi
          .updateOrg(params)
          .then((res) => {
            return res
          })
          .catch(() => {})
      },

      onOk() {
        this.btnLoading = true
        if (this.currentRow.roleType == 1) {
          let params = {
            configId: this.currentRow.id,
            isAssess: this.currentRow.isAssess,
            isTaskAssess: this.currentRow.isTaskAssess,
            updateAll: this.isSynchronize == 0 ? false : true
          }
          sopApi
            .userAuditPlatformSwitch(params)
            .then((res) => {
              this.updateClass().then((res) => {
                this.$refs.info.hide()
                this.$Message.success('修改成功！')
                this.$emit('success')
                this.btnLoading = false
              })
            })
            .catch(() => {})
        } else {
          this.updateOrg().then((res) => {
            this.updateClass().then((res) => {
              this.$refs.info.hide()
              this.$Message.success('修改成功！')
              this.$emit('success')
              this.btnLoading = false
            })
          })
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .taskInfo {
    display: flex;
    flex-wrap: wrap;

    p {
      width: 49%;
      line-height: 2;

      span.sp1 {
        display: inline-block;
        width: 135px;
        text-align: right;
      }

      span.sp2 {
        em {
          font-style: normal;
          color: rgb(51, 102, 255);
          cursor: pointer;
        }
      }
    }
  }

  .downBox {
    border: 1px solid #e8e8e8;
    padding: 20px;
    margin-top: 10px;
  }

  .tips {
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    background: rgba(254, 252, 236, 1);

    .tips_icon {
      font-size: 20px;
      color: #0f42a7;
      vertical-align: sub;
    }
  }

  .conpany_sop_edit {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    width: 60%;
    padding: 30px 0;

    .taskSwitch {
      display: flex;
      flex-direction: row;
      justify-content: space-around;

      span {
        margin-right: 5px;
      }
    }
  }

  .sync {
    padding-top: 20px;
    border-top: 1px solid #e8e8e8;

    .sync_txt {
      line-height: 32px;
      font-size: 16px;
    }

    .sync_tips {
      font-size: 12px;
      color: #999;
      margin-top: 10px;
    }
  }
</style>
